﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// LMediaPlayer.xaml 的交互逻辑
    /// </summary>
    public partial class LMediaPlayer : UserControl
    {
        public LMediaPlayer()
        {
            InitializeComponent();
        }

        private void ReadSoundInfo()
        {
            byte[] b = new byte[128];
            string sTitle;
            string sSinger;
            string sAlbum;
            string sYear;
            string sComm;
            FileStream fs = new FileStream(this.Source.LocalPath, FileMode.Open);
            fs.Seek(-128, SeekOrigin.End);
            fs.Read(b, 0, 128);
            bool isSet = false;
            String sFlag = System.Text.Encoding.Default.GetString(b, 0, 3);
            if (sFlag.CompareTo("TAG") == 0)
            {
                isSet = true;
            }

            StringBuilder sb = new StringBuilder();

            if (isSet)
            {
                var trimChars = new char[] { '\0', '\b' };
                //get title of song;
                sTitle = System.Text.Encoding.Default.GetString(b, 3, 30).TrimEnd(trimChars);
                if (string.IsNullOrWhiteSpace(sTitle) == false)
                    sb.Append($"声音标题> {sTitle}\r\n");
                //get singer;
                sSinger = System.Text.Encoding.Default.GetString(b, 33, 30).TrimEnd(trimChars);
                if (string.IsNullOrWhiteSpace(sSinger) == false)
                    sb.Append($"艺 术 家> {sSinger}\r\n");
                //get album;
                sAlbum = System.Text.Encoding.Default.GetString(b, 63, 30).TrimEnd(trimChars);
                if (string.IsNullOrWhiteSpace(sAlbum) == false)
                    sb.Append($"唱片标题> {sAlbum}\r\n");
                //get Year of publish;
                sYear = System.Text.Encoding.Default.GetString(b, 93, 4).TrimEnd(trimChars);
                if (string.IsNullOrWhiteSpace(sYear) == false)
                    sb.Append($"发 行 年> {sYear}\r\n");
                //get Comment;
                sComm = System.Text.Encoding.Default.GetString(b, 97, 30).TrimEnd(trimChars);
                if (string.IsNullOrWhiteSpace(sComm) == false)
                    sb.Append($"备　　注> {sComm}");

            }

            tbInfo.Text = sb.ToString();

            if (string.IsNullOrWhiteSpace(tbInfo.Text))
            {
                tbInfo.Visibility = Visibility.Collapsed;
            }
            else tbInfo.Visibility = Visibility.Visible;
        }

        public Uri Source
        {
            get { return this.videoelement.Source; }
            set
            {
                try
                {
                    this.videoelement.Source = value;
                    btnPlay.Tag = "Pause";
                    ReadSoundInfo();
                    btnPlay_Click(this, null);
                }
                catch (Exception ex)
                {
                    LMessageBox.Show(ex.Message, Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            if (this.btnPlay.Tag.ToString() != "Play")
            {
                this.videoelement.Play();
                this.btnPlay.Tag = "Play";
                playPath.Fill = Brushes.Green;
            }
            else
            {
                this.videoelement.Pause();
                this.btnPlay.Tag = "Pause";
                playPath.Fill = Brushes.Gray;
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            this.videoelement.Stop();
            this.btnPlay.Tag = "Pause";
            playPath.Fill = Brushes.Gray;
        }

        private void btnForward_Click(object sender, RoutedEventArgs e)
        {
            this.videoelement.Position = this.videoelement.Position + TimeSpan.FromSeconds(10);
        }

        private void btnBackWard_Click(object sender, RoutedEventArgs e)
        {
            this.videoelement.Position = this.videoelement.Position - TimeSpan.FromSeconds(10);
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog opnDialogue =
                new System.Windows.Forms.OpenFileDialog();
            opnDialogue.Filter = "支持的格式|*.mp3;*.wma;*.wav;*.wmv|Mp3(*.mp3)|*.mp3|WMA (*.wma)|*.wma|Wav (*.wav)|*.wav|WMV 视频文件(*.wmv)|*.wmv";
            if (opnDialogue.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Source = new Uri(opnDialogue.FileName);
            }
        }
    }
}
